package com.lanou3g.bookstore.user.dao;

import com.lanou3g.bookstore.user.domain.AdminUser;
import com.lanou3g.bookstore.user.domain.User;
import com.lanou3g.bookstore.util.util.JDBCUtil;
import com.lanou3g.bookstore.util.util.MyResultSet;
import com.lanou3g.bookstore.util.util.MyStatement;

public class UserDao {
    //登录/注册
    public User findByUsername(String username) {
        String sql = "select * from tb_user where username = ?";
        MyStatement statement = JDBCUtil.getStatement(sql);
        statement.setString(1, username);
        MyResultSet rs = statement.executeQuery();
        User user = null;
        if (rs.next()) {
            user = new User();
            user.setUsername(rs.getString("username"))
                    .setPwd(rs.getString("password"))
                    .setEmail(rs.getString("email"))
                    .setUid(rs.getString("uid"));
        }
        rs.close();
        statement.close();
        return user;
    }

    //找email
    public User findByEmail(String email) {
        String sql = "select * from tb_user where email = ?";
        MyStatement statement = JDBCUtil.getStatement(sql);
        statement.setString(1, email);
        MyResultSet rs = statement.executeQuery();
        User user = null;
        if (rs.next()) {
            user = new User();
            user.setUsername(rs.getString("username"))
                    .setPwd(rs.getString("password"))
                    .setEmail(rs.getString("email"));
        }
        rs.close();
        statement.close();
        return user;
    }


    //成功注册
    public void add(User user) {
        String sql = "insert into tb_user (uid, username, `password`, email) values " +
                "(?, ?, ?, ?)";
        MyStatement statement = JDBCUtil.getStatement(sql);
        statement.setString(1, String.valueOf(user.getUid()));
        statement.setString(2, user.getUsername());
        statement.setString(3, user.getPwd());
        statement.setString(4, user.getEmail());
        statement.executeUpdate();
        statement.close();
    }

//    //找id最大值
//    public Integer findMaxUid() {
//        String sql = "select max(uid) from tb_user";
//        MyStatement statement = JDBCUtil.getStatement(sql);
//        MyResultSet rs = statement.executeQuery();
//        Integer uid = null;
//        if (rs.next()) {
//            uid = rs.getInt("max(uid)");
//        }
//        rs.close();
//        statement.close();
//        return uid;
//    }

    public AdminUser login(String username, String pwd) {
        String sql = "select * from adminuser where username = ? and pwd = ?";
        MyStatement statement = JDBCUtil.getStatement(sql);
        statement.setString(1, username);
        statement.setString(2, pwd);
        MyResultSet rs = statement.executeQuery();
        AdminUser user = null;
        if (rs.next()) {
            user = new AdminUser();
            user.setUsername(rs.getString("username"))
                    .setPwd(rs.getString("pwd"));
        }
        statement.close();
        JDBCUtil.closeConn();
        return user;
    }

}
